//
//  BaseNavigationController.m
//  Normal
//
//  Created by 8b410-2 on 16/12/9.
//  Copyright © 2016年 abb. All rights reserved.
//

#import "OSCBaseNavigationController.h"
#import "UIColor+Common.h"
@interface OSCBaseNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation OSCBaseNavigationController

+(void)initialize {
    [super initialize];
    //设置Nav的背景色和title色
    UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
    NSDictionary *textAttributes = nil;
    
    [navigationBarAppearance setTintColor:[UIColor themeColor]];//返回按钮的箭头颜色
    
    textAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:18],
                       NSForegroundColorAttributeName: [UIColor blackColor]};
    //设置导航栏的背景图片，否则push的时候有黑影
    [navigationBarAppearance setTitleTextAttributes:textAttributes];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.interactivePopGestureRecognizer.delegate = self;
    
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (self.childViewControllers.count >= 1 ) {//如果view控制器不是最早push进来的子控制器
        viewController.hidesBottomBarWhenPushed = YES;
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *image = [[UIImage imageNamed:@"navigation_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [backButton setImage:image forState:UIControlStateNormal];
        [backButton setTintColor:[UIColor themeColor]];
        backButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
        backButton.frame = CGRectMake(0, 0, 20, 20);
        backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
        [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        //设置返回按钮
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    }
    [super pushViewController:viewController animated:animated];
}
/**
 *  返回按钮点击
 */
-(void)back {
    [self popViewControllerAnimated:YES];
}

#pragma mark - <UIGestureRecognizerDelegate>
/**
 *  手势识别器对象会调用这个代理方法来决定手势是否有效
 *
 *  @return yes：手势有效 no：手势无效
 */
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    //如果子控制器是1的时候如果手势有效的话，有个bug，下次push不进去
    //手势何时有效：当导航控制器的子控制器的个数 > 1有效
    return self.childViewControllers.count > 1;
}

@end
